<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
 


 
 
 


 <script type="text/javascript">
 
 
 
 var codesite_token = "c13d00298dea02cdeaeed2311d9beb5c";
 
 
 var logged_in_user_email = "Andreykyz@gmail.com";
 
 </script>
 
 
 <title>UserDocumentation - 
 html5lib -
 
 Documentation for using the library - Project Hosting on Google Code</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
 <link type="text/css" rel="stylesheet" href="UserDocumentation_files/ph_core.css">
 
 <link type="text/css" rel="stylesheet" href="UserDocumentation_files/ph_detail.css">
 
 
 
 
<!--[if IE]>
 <link type="text/css" rel="stylesheet" href="http://www.gstatic.com/codesite/ph/2429932258724909799/css/d_ie.css" >
<![endif]-->
 <style type="text/css">
 .menuIcon.off { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 -42px }
 .menuIcon.on { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 -28px }
 .menuIcon.down { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 0; }
 .results th a, .results th a:visited { background: url(http://www.gstatic.com/codesite/ph/images/downarrow.gif) no-repeat top right }
 </style>
<script style="display: none;" type="text/javascript" src="UserDocumentation_files/ga.js"></script></head><body class="t6">
 <div id="gaia">
 
 <span>
 
 <b>Andreykyz@gmail.com</b>
 
 
 | <a href="http://code.google.com/u/Andreykyz/" id="projects-dropdown" onclick="return false;">My favorites</a><img class="menuIcon off" id="menuIcon-projects-dropdown" src="UserDocumentation_files/cleardot.gif" height="14" width="14">
 
 | <a href="http://code.google.com/u/Andreykyz/" onclick="_CS_click('/gb/ph/profile');" title="Profile, Updates, and Settings">Profile</a>
 | <a href="https://www.google.com/accounts/Logout?continue=http%3A%2F%2Fcode.google.com%2Fp%2Fhtml5lib%2Fwiki%2FUserDocumentation" onclick="_CS_click('/gb/ph/signout');">Sign out</a>
 
 </span>

 </div>
 <div class="gbh" style="left: 0pt;"></div>
 <div class="gbh" style="right: 0pt;"></div>
 
 
 <div style="height: 1px;"></div>
<!--[if IE 6]>
<div style="text-align:center;">
Support browsers that contribute to open source, try <a href="http://www.firefox.com">Firefox</a> or <a href="http://www.google.com/chrome">Google Chrome</a>.
</div>
<![endif]-->
 <table style="margin: 20px 0px 0px; padding: 0px; width: 100%;" cellpadding="0" cellspacing="0">
 <tbody><tr style="height: 58px;">
 
 <td style="width: 55px; text-align: center;">
 <a href="http://code.google.com/p/html5lib/">
 
 
 
 <img src="UserDocumentation_files/defaultlogo.png" alt="Project Logo">
 
 
 
 </a>
 </td>
 
 <td style="padding-left: 0.8em;">
 
 <div id="pname" style="margin: 0px 0px -3px;">
 <a href="http://code.google.com/p/html5lib/" style="text-decoration: none; color: rgb(0, 0, 0);">html5lib</a>
 </div>
 
 <div id="psum">
 <i><a id="project_summary_link" href="http://code.google.com/p/html5lib/" style="text-decoration: none; color: rgb(0, 0, 0);">Library for working with HTML documents</a></i>
 </div>
 
 </td>
 <td style="white-space: nowrap; text-align: right;">
 
 <form action="/hosting/search">
 <input size="30" name="q">
 <input name="projectsearch" value="Search projects" type="submit">
 </form>
 
 </td></tr>
 </tbody></table>



 
<table id="mt" width="100%" cellpadding="0" cellspacing="0" border="0">
 <tbody><tr>
 <th onclick="if (!cancelBubble) _go('/p/html5lib/');">
 <div class="tab inactive">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <a onclick="cancelBubble=true;" href="http://code.google.com/p/html5lib/">Project&nbsp;Home</a>
 </div>
 </div>
 </th><td>&nbsp;&nbsp;</td>
 
 
 
 
 <th onclick="if (!cancelBubble) _go('/p/html5lib/downloads/list');">
 <div class="tab inactive">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <a onclick="cancelBubble=true;" href="http://code.google.com/p/html5lib/downloads/list">Downloads</a>
 </div>
 </div>
 </th><td>&nbsp;&nbsp;</td>
 
 
 
 
 
 <th onclick="if (!cancelBubble) _go('/p/html5lib/w/list');">
 <div class="tab active">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <a onclick="cancelBubble=true;" href="http://code.google.com/p/html5lib/w/list">Wiki</a>
 </div>
 </div>
 </th><td>&nbsp;&nbsp;</td>
 
 
 
 
 
 <th onclick="if (!cancelBubble) _go('/p/html5lib/issues/list');">
 <div class="tab inactive">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <a onclick="cancelBubble=true;" href="http://code.google.com/p/html5lib/issues/list">Issues</a>
 </div>
 </div>
 </th><td>&nbsp;&nbsp;</td>
 
 
 
 
 
 <th onclick="if (!cancelBubble) _go('/p/html5lib/source/checkout');">
 <div class="tab inactive">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <a onclick="cancelBubble=true;" href="http://code.google.com/p/html5lib/source/checkout">Source</a>
 </div>
 </div>
 </th><td>&nbsp;&nbsp;</td>
 
 
 <td width="100%">&nbsp;</td>
 </tr>
</tbody></table>
<table class="st" width="100%" cellpadding="0" cellspacing="0" align="center" border="0">
 <tbody><tr>
 
 
 
 <td>
 <div class="issueDetail">
<div class="isf">
 
 <span class="inIssueList"> 
 <span>Search</span>
 <form action="/p/html5lib/w/list" method="GET" style="display: inline;">
 <select id="can" name="can" style="font-size: 92%;">
 <option disabled="disabled">Search within:</option>
 
 <option value="1">&nbsp;All wiki pages</option>
 <option value="3">&nbsp;Featured pages</option>
 <option value="2" selected="selected">&nbsp;Current pages</option>
 
 
 <option value="4">&nbsp;Deprecated pages</option>
 
 </select>
 <span>for</span>
 <span id="qq"><input size="32" id="q" name="q" style="font-size: 92%;" type="text"></span>
 
 
 <input value="Search" style="font-size: 92%;" type="submit">
 </form>
 </span>

 
 
 
 
 
 
 

</div>
</div>

 </td>
 
 
 
 
 
 
 
 
 <td class="bevel-right" height="4" valign="top" align="right">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 </td>
 </tr>
</tbody></table>
<script type="text/javascript">
 var cancelBubble = false;
 function _go(url) { document.location = url; }
</script>


<div id="maincol">

 
<!-- IE -->






 <style type="text/css">
 .artifactcomment, #commentform {
 margin: .5em 0 0 0;
 padding: .3em 0 .3em .3em;
 border-top: 3px solid #c3d9ff;
 }
 #commentform { padding-top: 1em; }
 .delcom { background: #e8e8e8 }
 .commentcontent { margin: 1em 0;}
 </style>


 <div id="wikicontent">
 <table width="100%" cellpadding="0" cellspacing="0" border="0">
 <tbody><tr>
 
 <td class="vt" id="wikimaincol" width="100%">
 
 <div style="float: right; width: 18em;" id="wikimeta">
 <div class="pmeta_bubble_bg">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner">
 <table style="padding: 5px;">
 <tbody><tr><td colspan="2" style="padding-bottom: 5px;">Updated <span title="Sun Sep  7 12:03:30 2008">Sep 07, 2008</span>
 
 by <a style="white-space: nowrap;" href="http://code.google.com/u/jgraham.html/">jgraham.html</a>
 </td></tr>
 
 <tr><th class="vt">Labels:</th>
 <td>
 
 <a href="http://code.google.com/p/html5lib/w/list?q=label:Documentation-User" title="End User Documentation">Documentation-User</a>
 
 </td>
 </tr>
 
 
 
 </tbody></table>
 </div>
 <div class="round1"></div>
 <div class="round2"></div>
 <div class="round4"></div>
 </div>
 </div>
 
 <div id="wikiheader" style="margin-bottom: 1em;">
 
 <span style="font-size: 120%; font-weight: bold;">UserDocumentation</span>
 &nbsp;
 
 
 <div style="font-style: italic; margin-top: 3px;">Documentation for using the library</div>
 
 </div>
 <h1><a name="Using_html5lib">Using html5lib</a></h1><h2><a name="Installation">Installation</a></h2><p><a name="Installation">html5lib is packaged with setuptools. To install it use: </a></p><pre class="prettyprint"><a name="Installation"><span class="pln">&nbsp;$ python setup</span><span class="pun">.</span><span class="pln">py install</span></a></pre><h2><a name="Tests">Tests</a></h2><p><a name="Tests">You
may wish to check that your installation has been a success by running
the testsuite. All the tests can be run by invoking runtests.py in the
tests/ directory or by running </a></p><pre class="prettyprint"><a name="Tests"><span class="pln">$ python setup</span><span class="pun">.</span><span class="pln">py test</span></a></pre><h2><a name="Parsing_HTML">Parsing HTML</a></h2><p><a name="Parsing_HTML">Simple usage follows this pattern: </a></p><pre class="prettyprint"><a name="Parsing_HTML"><span class="kwd">import</span><span class="pln"> html5lib<br>f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"mydocument.html"</span><span class="pun">)</span><span class="pln"><br>parser </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">()</span><span class="pln"><br>document </span><span class="pun">=</span><span class="pln"> parser</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">f</span><span class="pun">)</span></a></pre><p><a name="Parsing_HTML">This
will return a tree in a custom "simpletree" format. More interesting is
the ability to use a variety of standard tree formats; currently
minidom, ElementTree and BeafutifulSoup formats are supported by
default. To do this you need to pass a TreeBuilder class as the "tree"
argument to the HTMLParser. For the built-in treebuilders this can be
conveniently obtained from the treebuilders.getTreeBuilder function
e.g. for minidom: </a></p><pre class="prettyprint"><a name="Parsing_HTML"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treebuilders<br><br>f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"mydocument.html"</span><span class="pun">)</span><span class="pln"><br>parser </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">=</span><span class="pln">treebuilders</span><span class="pun">.</span><span class="pln">getTreeBuilder</span><span class="pun">(</span><span class="str">"dom"</span><span class="pun">))</span><span class="pln"><br>minidom_document </span><span class="pun">=</span><span class="pln"> parser</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">f</span><span class="pun">)</span></a></pre><p><a name="Parsing_HTML">For
a BeautifulSoup tree replace the string "dom" with "beautifulsoup". For
ElementTree the procedure is slightly more involved as there are many
libraries that support the ElementTree API. Therefore getTreeBuilder
accepts a second argument which is the ElementTree implementation that
is desired (in the future this may be extended, for example to allow
multiple DOM libraries to be used): </a></p><pre class="prettyprint"><a name="Parsing_HTML"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treebuilders<br></span><span class="kwd">from</span><span class="pln"> xml</span><span class="pun">.</span><span class="pln">etree </span><span class="kwd">import</span><span class="pln"> cElementTree<br><br>f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"mydocument.html"</span><span class="pun">)</span><span class="pln"><br>parser </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">=</span><span class="pln">treebuilders</span><span class="pun">.</span><span class="pln">getTreeBuilder</span><span class="pun">(</span><span class="str">"etree"</span><span class="pun">,</span><span class="pln"> cElementTree</span><span class="pun">))</span><span class="pln"><br>etree_document </span><span class="pun">=</span><span class="pln"> parser</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">f</span><span class="pun">)</span></a></pre><p><a name="Parsing_HTML">If
you are using the excellent lxml library, using the generic etree
treebuilder described above with fail. Instead you must use the lxml
builder: </a></p><pre class="prettyprint"><a name="Parsing_HTML"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treebuilders<br></span><span class="kwd">from</span><span class="pln"> lxml </span><span class="kwd">import</span><span class="pln"> etree<br><br>f </span><span class="pun">=</span><span class="pln"> open</span><span class="pun">(</span><span class="str">"mydocument.html"</span><span class="pun">)</span><span class="pln"><br>parser </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">=</span><span class="pln">treebuilders</span><span class="pun">.</span><span class="pln">getTreeBuilder</span><span class="pun">(</span><span class="str">"lxml"</span><span class="pun">))</span><span class="pln"><br>etree_document </span><span class="pun">=</span><span class="pln"> parser</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">f</span><span class="pun">)</span></a></pre><h3><a name="SAX_Events">SAX Events</a></h3><p><a name="SAX_Events">The
WHATWG spec is not very streaming-friendly as it requires rearrangement
of subtrees in some situations. However html5lib allows SAX events to
be created from a DOM tree using html5lib.treebuilders.dom.dom2sax </a></p><h3><a name="Character_encoding">Character encoding</a></h3><p><a name="Character_encoding">
Parsed trees are always Unicode. However a large variety of input
encodings are supported. The encoding of the document is determined in
the following way: </a></p><ul><li><a name="Character_encoding">The encoding may be explicitly specified by passing the name of the encoding as the encoding parameter to HTMLParser.parse </a></li></ul><ul><li><a name="Character_encoding">If
no encoding is specified, the parser will attempt to detect the
encoding from a &lt;meta&gt; element in the first 512 bytes of the
document (this is only a partial implementation of the current HTML 5
specification) </a></li></ul><ul><li><a name="Character_encoding">If
no encoding can be found and the chardet library is available, an
attempt will be made to sniff the encoding from the byte pattern </a></li></ul><ul><li><a name="Character_encoding">If all else fails, the default encoding (usually Windows-1252) will be used  </a></li></ul><h4><a name="Examples">Examples</a></h4><p><a name="Examples">Explicit encoding specification: </a></p><pre class="prettyprint"><a name="Examples"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">import</span><span class="pln"> urllib2<br>p </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">()</span><span class="pln"><br>p</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">urllib2</span><span class="pun">.</span><span class="pln">urlopen</span><span class="pun">(</span><span class="str">"http://yahoo.co.jp"</span><span class="pun">,</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"euc-jp"</span><span class="pun">).</span><span class="pln">read</span><span class="pun">())</span></a></pre><p><a name="Examples">Automatic detection from a meta element: </a></p><pre class="prettyprint"><a name="Examples"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">import</span><span class="pln"> urllib2<br>p </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">()</span><span class="pln"><br>p</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">urllib2</span><span class="pun">.</span><span class="pln">urlopen</span><span class="pun">(</span><span class="str">"http://www.mozilla-japan.org/"</span><span class="pun">).</span><span class="pln">read</span><span class="pun">())</span></a></pre><h2><a name="Sanitizing_Tokenizer">Sanitizing Tokenizer</a></h2><p><a name="Sanitizing_Tokenizer">When
building web applications it is often necessary to remove unsafe markup
and CSS from user entered content. html5lib provides a custom tokenizer
for this purpose. It only allows known safe element tokens through and
converts others to text. Similarly, a variety of unsafe CSS constructs
are removed from the stream. For more details on the default
configuration of the sanitizer, see </a><a href="http://wiki.whatwg.org/wiki/Sanitization_rules" rel="nofollow">http://wiki.whatwg.org/wiki/Sanitization_rules</a> The sanitizer can be used by passing it as the tokenizer argument to the parser: </p><pre class="prettyprint"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> sanitizer<br><br>p </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">(</span><span class="pln">tokenizer</span><span class="pun">=</span><span class="pln">sanitizer</span><span class="pun">.</span><span class="typ">HTMLSanitizer</span><span class="pun">)</span><span class="pln"><br>p</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="str">"&lt;script&gt;alert('foo');&lt;/script&gt;"</span><span class="pun">)</span></pre><h2><a name="XML_Parsing">XML Parsing</a></h2><p><a name="XML_Parsing">html5lib
comes with two classes for parsing XML documents (one specifically for
XHTML). These are essentially the HTMLParser without the HTML-specific
logic. The two available classes are html5lib.XMLParser and
html5lib.XHTMLParser. </a></p><h3><a name="Example">Example</a></h3><p><a name="Example">Parsing a Atom feed: </a></p><pre class="prettyprint"><a name="Example"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">import</span><span class="pln"> urllib2<br>p </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">XMLParser</span><span class="pun">()</span><span class="pln"><br>p</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">urllib2</span><span class="pun">.</span><span class="pln">urlopen</span><span class="pun">(</span><span class="str">"http://blog.whatwg.org/feed/"</span><span class="pun">).</span><span class="pln">read</span><span class="pun">())</span></a></pre><h2><a name="Treewalkers">Treewalkers</a></h2><p><a name="Treewalkers">Treewalkers
provide a streaming view of a tree. They are useful for filtering and
serializing the stream. html5lib provides a variety of treewalkers for
working with different tree types. For example, to stream a dom tree: </a></p><pre class="prettyprint"><a name="Treewalkers"><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treewalkers<br>walker </span><span class="pun">=</span><span class="pln"> treewalkers</span><span class="pun">.</span><span class="pln">getTreeWalker</span><span class="pun">(</span><span class="str">"dom"</span><span class="pun">)</span><span class="pln"><br><br>stream </span><span class="pun">=</span><span class="pln"> walker</span><span class="pun">(</span><span class="pln">dom_tree</span><span class="pun">)</span><span class="pln"> </span><span class="com">#stream is an iterable representing each token in the</span><span class="pln"><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="com">#tree</span></a></pre><p><a name="Treewalkers">Treewalkers
are avaliable for all the tree types supported by the HTMLParser plus
xml.dom.pulldom ("pulldom"), genshi streams ("genshi") and a
lxml-optimized elementtree ("lxml"). As for the treebulders,
treewalkers.getTreeWalker takes a second argument implementation
containing a object implementing the ElementTree API. </a></p><h3><a name="Sanitization_using_treewalkers">Sanitization using treewalkers</a></h3><p><a name="Sanitization_using_treewalkers">You
may wish to sanitize content from an which has been parsed into a tree
by some other code. This may be done using the sanitizer filter: </a></p><pre class="prettyprint"><a name="Sanitization_using_treewalkers"><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treewalkers</span><span class="pun">,</span><span class="pln"> filters<br></span><span class="kwd">from</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="pln">filters </span><span class="kwd">import</span><span class="pln"> sanitizer<br><br>walker </span><span class="pun">=</span><span class="pln"> treewalkers</span><span class="pun">.</span><span class="pln">getTreeWalker</span><span class="pun">(</span><span class="str">"dom"</span><span class="pun">)</span><span class="pln"><br><br>stream </span><span class="pun">=</span><span class="pln"> walker</span><span class="pun">(</span><span class="pln">dom_tree</span><span class="pun">)</span><span class="pln"><br>clean_stream </span><span class="pun">=</span><span class="pln"> sanitizer</span><span class="pun">.</span><span class="typ">Filter</span><span class="pun">(</span><span class="pln">stream</span><span class="pun">)</span></a></pre><h3><a name="Serialization_of_Streams">Serialization of Streams</a></h3><p><a name="Serialization_of_Streams">html5lib
provides HTML and XHML serializers which work on streams produced by
the treewalkers. These are implemented as generators with each item in
the generator representing a single tag. A full example of parsing and
serializing content looks like: </a></p><pre class="prettyprint"><a name="Serialization_of_Streams"><span class="kwd">import</span><span class="pln"> html5lib<br></span><span class="kwd">from</span><span class="pln"> html5lib </span><span class="kwd">import</span><span class="pln"> treebuilders</span><span class="pun">,</span><span class="pln"> treewalkers</span><span class="pun">,</span><span class="pln"> serializer<br></span><span class="kwd">from</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="pln">filters </span><span class="kwd">import</span><span class="pln"> sanitizer<br><br>p </span><span class="pun">=</span><span class="pln"> html5lib</span><span class="pun">.</span><span class="typ">HTMLParser</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">=</span><span class="pln">treebuilders</span><span class="pun">.</span><span class="pln">getTreeBuilder</span><span class="pun">(</span><span class="str">"dom"</span><span class="pun">))</span><span class="pln"><br><br>dom_tree </span><span class="pun">=</span><span class="pln"> p</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="str">"&lt;p&gt;&lt;strong&gt;Hello&lt;/strong&gt; World&lt;/p&gt;"</span><span class="pun">)</span><span class="pln"><br><br>walker </span><span class="pun">=</span><span class="pln"> treewalkers</span><span class="pun">.</span><span class="pln">getTreeWalker</span><span class="pun">(</span><span class="str">"dom"</span><span class="pun">)</span><span class="pln"><br><br>stream </span><span class="pun">=</span><span class="pln"> walker</span><span class="pun">(</span><span class="pln">dom_tree</span><span class="pun">)</span><span class="pln"><br><br>s </span><span class="pun">=</span><span class="pln"> serializer</span><span class="pun">.</span><span class="pln">htmlserializer</span><span class="pun">.</span><span class="typ">HTMLSerializer</span><span class="pun">(</span><span class="pln">omit_optional_tags</span><span class="pun">=</span><span class="kwd">False</span><span class="pun">)</span><span class="pln"><br>output_generator </span><span class="pun">=</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">serialize</span><span class="pun">(</span><span class="pln">stream</span><span class="pun">)</span><span class="pln"><br><br></span><span class="kwd">for</span><span class="pln"> item </span><span class="kwd">in</span><span class="pln"> output_generator</span><span class="pun">:</span><span class="pln"><br>&nbsp; &nbsp; </span><span class="kwd">print</span><span class="pln"> item<br><br></span><span class="pun">&lt;</span><span class="pln">html</span><span class="pun">&gt;</span><span class="pln"><br></span><span class="pun">&lt;</span><span class="pln">head</span><span class="pun">&gt;</span><span class="pln"><br></span><span class="pun">&lt;</span><span class="str">/head&gt;<br>&lt;body&gt;<br>&lt;p&gt;<br>&lt;strong&gt;<br>Hello<br>&lt;/</span><span class="pln">strong</span><span class="pun">&gt;</span><span class="pln"><br>&nbsp;<br></span><span class="typ">World</span><span class="pln"><br></span><span class="pun">&lt;</span><span class="str">/p&gt;<br>&lt;/</span><span class="pln">body</span><span class="pun">&gt;</span><span class="pln"><br></span><span class="pun">&lt;/</span><span class="pln">html</span><span class="pun">&gt;</span><span class="pln"><br></span></a></pre><h3><a name="Validation">Validation</a></h3><p><a name="Validation">The html5lib.filters.validator module contains a partial implementation of a HTML5 validator. </a></p><h1><a name="Bugs">Bugs</a></h1><p><a name="Bugs">Please report any bugs on the issue tracker: </a><a href="http://code.google.com/p/html5lib/issues/list" rel="nofollow">http://code.google.com/p/html5lib/issues/list</a> </p><h1><a name="Get_Involved">Get Involved</a></h1><p><a name="Get_Involved">Contributions
to code or documenation are actively encouraged. Submit patches to the
issue tracker or discuss changes on irc in the #whatwg channel on
freenode.net </a></p>
<a name="Get_Involved"> </a></td> 
 </tr>
 </tbody></table>
 </div>


 
 <br>
 <div id="commentlist">
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

<div class="collapse">


 

 

 

 

 

 
 <div class="artifactcomment">
 
 
 
 
 
 <span class="author">Comment
 
 by
 
 <a style="white-space: nowrap;" href="http://code.google.com/u/serjux/">serjux</a>,
 
 </span>
 <span class="date" title="Wed Jun 18 18:25:04 2008">Jun 18, 2008</span>
 <div>
<div class="commentcontent">
<p>Some practically examples of using html5lib with minidom for example
should be nice , some complete code example would be nice </p>
</div>
 
 
 </div>
 </div>
 

 
 <div class="artifactcomment">
 
 
 
 
 
 <span class="author">Comment
 
 by
 
 <a style="white-space: nowrap;" href="http://code.google.com/u/@WRVWS1NWARRMXAV8/">calid...@gmail.com</a>,
 
 </span>
 <span class="date" title="Wed Aug 12 04:17:40 2009">Aug 12, 2009</span>
 <div>
<div class="commentcontent">
<p>what does parser() return? and how to manipulate it? </p>
</div>
 
 
 </div>
 </div>
 

</div>

 <script type="text/javascript">
 function delComment(sequence_num, delete_mode) {
 var f = document.forms["delcom"];
 f.sequence_num.value = sequence_num;
 f.mode.value = delete_mode;
 f.submit();
 return false;
 }
 </script>


 </div>
 
 
 
 
 
 <div id="commentform">
 <form action="../w/detail.do" method="post">
 <table>
 <tbody><tr><td class="vt">
 <input name="pagename" value="UserDocumentation" type="hidden">
 <input name="token" value="c13d00298dea02cdeaeed2311d9beb5c" type="hidden">
 <div>Enter a comment:</div>
 <textarea name="content" rows="6" cols="80"></textarea><br><br>
 <input name="submit" value="Submit" type="submit">
 </td>
 <td class="vt">
 <div id="whb" class="tip" style="width: 15em;">
 <div class="round4"></div>
 <div class="round2"></div>
 <div class="round1"></div>
 <div class="box-inner closed" id="entrybubble">
 <a class="ifClosed" href="#whb" style="float: right;" onclick="_toggleHidden(this); document.getElementById('wikihelp').value='opened'">show</a>
 <a class="ifOpened" href="#whb" style="float: right;" onclick="_toggleHidden(this); document.getElementById('wikihelp').value='closed'">hide</a>
 <b>Wiki markup help</b>
 <div class="ifOpened">
 <div style="padding: 2px; font-size: 96%;">
 =Heading1=<br>
 ==Heading2==<br>
 ===Heading3===<br>
 <br>
 *bold* &nbsp; &nbsp;
 _italic_<br>
 `inline code`<br>
 escape: `*`<br>
 <br>
 Indent lists 2 spaces:<br>
 &nbsp;&nbsp;* bullet item<br>
 &nbsp;&nbsp;# numbered list<br>
 <br>
 {{{<br>
 verbatim code block<br>
 }}}<br>
 <br>
 Horizontal rule<br>
 ----
 <br>
 <br>
 WikiWordLink<br>
 [http://domain/page&nbsp;label]<br>
 http://domain/page<br>
 <br>
 || table || cells ||<br>
 
 </div><br>
 <a href="http://code.google.com/p/support/wiki/WikiSyntax" target="new">More
 examples</a>
 <a href="http://code.google.com/p/support/wiki/WikiSyntax" target="new"><img src="UserDocumentation_files/tearoff_icon.gif" height="16" width="16"></a>
 </div>
 </div>
 <div class="round1"></div>
 <div class="round2"></div>
 <div class="round4"></div>
</div>
<input name="wikihelp" id="wikihelp" value="closed" type="hidden">

 </td></tr>
 </tbody></table>
 </form>
 </div>
 
 
 

<form name="delcom" action="../w/delComment.do" method="POST">
 <input name="sequence_num" value="" type="hidden">
 <input name="mode" value="" type="hidden">
 <input name="pagename" value="UserDocumentation" type="hidden">
 <input name="token" value="c13d00298dea02cdeaeed2311d9beb5c" type="hidden">
</form>



<script src="UserDocumentation_files/prettify.js"></script>
<script type="text/javascript">
 prettyPrint();
</script>
<script type="text/javascript" src="UserDocumentation_files/dit_scripts_20081013.js"></script>



  
 <script type="text/javascript" src="UserDocumentation_files/core_scripts_20081103.js"></script>
 <script type="text/javascript" src="UserDocumentation_files/codesite_product_dictionary_ph.js"></script>
 
 
 
 </div>
<div id="footer" dir="ltr">
 
 <div class="text">
 
 ©2009 Google -
 <a href="http://code.google.com/">Code Home</a> -
 <a href="http://code.google.com/projecthosting/terms.html">Terms of Service</a> -
 <a href="http://www.google.com/privacy.html">Privacy Policy</a> -
 <a href="http://code.google.com/more/">Site Directory</a> -
 <a href="http://code.google.com/p/support/">Project Hosting Help</a>
 
 </div>
</div>
<script type="text/javascript">
/**
 * Reports analytics.
 * It checks for the analytics functionality (window._gat) every 100ms
 * until the analytics script is fully loaded in order to invoke siteTracker.
 */
function _CS_reportAnalytics() {
 window.setTimeout(function() {
 if (window._gat) {
 try {
 siteTracker = _gat._getTracker(CS_ANALYTICS_ACCOUNT);
 siteTracker._trackPageview();
 } catch (e) {}
 
 } else {
 _CS_reportAnalytics();
 }
 }, 100);
}
</script>

 
 
 <div class="hostedBy" style="margin-top: -20px;">
 <span style="vertical-align: top;">Hosted by</span>
 <a href="http://code.google.com/hosting/">
 <img src="UserDocumentation_files/google_code_tiny.png" alt="Google Code" height="24" width="107">
 </a>
 </div>
 
 
 
 


 
 <div style="position: absolute; top: 19px; left: 1066px; display: none;" id="menuDiv-projects-dropdown" class="menuDiv instance0"><div class="menuCategory controls first"><hr class="menuSeparator"><a href="http://code.google.com/more/" style="display: block;" class="menuItem">Find developer products...</a><a href="http://code.google.com/hosting/" style="display: block;" class="menuItem">Find open source projects...</a></div></div></body></html>